<template>
    <el-checkbox
        v-model="currentValue"
        :disabled="disabled"
        @change="handleChangeValue"
    ><slot></slot></el-checkbox>
</template>

<script>
import {isNullOrUndefined, safeGet} from "../../../core";

export default {
    name: "SksCheckbox",
    props:{
        clearable: Boolean,
        placeholder: String,
        //form表单数据
        formData:[Object],
        //属性名
        propName:[String],

        //取消勾选时候的值
        activeValue: {
            type: [Boolean, String, Number],
            default: 0
        },
        //勾选时候的值
        inactiveValue: {
            type: [Boolean, String, Number],
            default: 1
        },

        disabled: {
            type: Boolean,
            default: false
        },
    },
    watch:{
        formData:{
            handler(val){
                if(isNullOrUndefined(this.formData)){
                    this.currentValue=false
                    return
                }
                if(isNullOrUndefined(this.propName)) {
                    this.currentValue=false
                    return
                }
                if(safeGet(this.formData,this.propName)!=this.currentValue){
                    this.currentValue=this.formData[this.propName]==1
                }
            },
            immediate: true,
            deep: true,
        }
    },
    data(){
        return {
            //
            currentValue: undefined,
        }
    },
    methods:{
        handleInputValue(){
            this.handleChangeValue()
        },
        handleChangeValue(){
            console.log("sksSwitch.handleChangeValue",this.formData,this.propName,this.currentValue)
            if(isNullOrUndefined(this.formData)){
                return
            }
            if(isNullOrUndefined(this.propName)) {
                return
            }
            let val = this.currentValue?this.inactiveValue:this.activeValue;
            this.formData[this.propName]= val

            this.$emit("input", val);
            this.$emit("change",val);
        },
    }

}
</script>

<style scoped>

</style>

<!--
                        <SksCheckbox
                            @change="handleQuery()"
                            :formData="queryParams.sksParams"
                            :propName="item.prop">
                        </SksCheckbox>
-->
